package com.zy.study.utils.MyException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zy.study.utils.ServiceResponse;

@ControllerAdvice
public class GlobalExceptionHandler {
	
	/**
	 * 处理自定义的业务异常
	 * @param req
	 * @param e
	 * @return
	 */
    @ExceptionHandler(value = BizException.class)  
    @ResponseBody  
	public ServiceResponse bizExceptionHandler(HttpServletRequest req, BizException e){
    	return ServiceResponse.createByErrorCodeMessage(e.getErrorCode(), e.getErrorMsg());
    }

	/**
	 * 处理空指针的异常
	 * @param req
	 * @param e
	 * @return
	 */
	@ExceptionHandler(value =NullPointerException.class)
	@ResponseBody
	public ServiceResponse exceptionHandler(HttpServletRequest req, NullPointerException e){
		return ServiceResponse.createByPARAM_parameter_error();
	}


    /**
        * 处理其他异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value =Exception.class)
	@ResponseBody
	public ServiceResponse exceptionHandler(HttpServletRequest req, Exception e){
       	return ServiceResponse.createByFailedMessage("服务器正忙，请稍后尝试");
    }
}
